var express = require('express');
var router = express.Router();
var mysqlquery = require("../db/mysql");
/* GET users listing. */
router.get('/register', function(req, res, next) {
  res.render('register.html');
});
router.get('/zhuce',async (req,res)=>{
      let {uname=""} = req.query;
      let sql = `select * from users where username = ${uname}`
      let [flag,data] = await mysqlquery(sql);
      if(flag && data.length>=1)
      {
        res.send("该用户已存在");
      }
      else
      {
        res.send("该账号可以注册");
      }
    
})
router.post('/toregister',async (req,res)=>{
      let {tel="",pass="",pass2=""} = req.body;
      if(tel=="" || pass=="" || pass2 == "" )
      {
        res.send("<script>alert('账号密码不得为空');window.location.href='/register'</script>");
      }
      else if(pass != pass2)
      {
        res.send("<script>alert('两次输入密码不一致');</script>");

      }
      else
      {
        //将新注册的用户插入表中
        let insertsql = `insert into users (username,password) values(${tel},${pass})`;
        let [flag,data] = await mysqlquery(insertsql);
        if(flag)
        {
        res.send("<script>alert('注册成功快去登录吧');window.location.href='/'</script>");

        }
      }
})
module.exports = router;
